package com.yupi.springbootinit.controller.production;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.dto.production.printingrecord.PrintingRecordAddRequest;
import com.yupi.springbootinit.model.dto.production.printingrecord.PrintingRecordQueryRequest;
import com.yupi.springbootinit.model.dto.production.printingrecord.PrintingRecordUpdateRequest;
import com.yupi.springbootinit.model.vo.production.PrintingRecordVO;
import com.yupi.springbootinit.service.PrintingRecordService;
import com.yupi.springbootinit.service.ProcessSheetService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * @Author:WwW
 * @Description
 * @Date: 2025/9/22  16:41
 */
@RestController
@RequestMapping("/printing-record")
@RequiredArgsConstructor
public class PrintingRecordController {

    @Resource
    private final PrintingRecordService printingRecordService;
    @Resource
    private ProcessSheetService processSheetService;

    @PostMapping("/add")
    @Operation(summary = "添加3D打印机台使用记录", operationId = "addRecord")
    public BaseResponse<Long> add(@RequestBody @Validated PrintingRecordAddRequest request) {
        Long id = printingRecordService.addPrintingRecord(request);
        return ResultUtils.success(id);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody @Validated PrintingRecordUpdateRequest request) {
        return ResultUtils.success(printingRecordService.updatePrintingRecord(request));
    }

    @PostMapping("/delete/{id}")
    @Operation(summary = "逻辑删除3D打印机台使用记录", operationId = "deleteRecord")

    public BaseResponse<Boolean> delete(@PathVariable Long id) {
        return ResultUtils.success(printingRecordService.deletePrintingRecord(id));
    }

    @GetMapping("/get/{id}")
    public BaseResponse<PrintingRecordVO> get(@PathVariable Long id) {
        return ResultUtils.success(printingRecordService.getPrintingRecordVO(id));
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<PrintingRecordVO>> listByPage(@RequestBody PrintingRecordQueryRequest request) {
        long current = request.getCurrent() <= 0 ? 1 : request.getCurrent();
        long size = request.getPageSize() <= 0 ? 10 : request.getPageSize();

        // 建议：打印记录上限 100（可配），附件上限 20
        final long MAX_PAGE_SIZE = 100L;
        ThrowUtils.throwIf(size > MAX_PAGE_SIZE, ErrorCode.PARAMS_ERROR, "pageSize 超过上限 " + MAX_PAGE_SIZE);

        request.setCurrent(current);
        request.setPageSize(size);
        return ResultUtils.success(printingRecordService.listPrintingRecordByPage(request));
    }

    /**
     * 根据产品名称查询订单信息(用于新建时自动填充订单编号)
     * @param productName 产品名称
     * @return 订单ID和订单编号
     */
    @GetMapping("/query-order-by-product")
    public BaseResponse<java.util.Map<String, Object>> queryOrderByProduct(@RequestParam String productName) {
        return ResultUtils.success(printingRecordService.queryOrderByProductName(productName));
    }

    /**
     * 根据产品名称查询工艺单信息
     */
    @GetMapping("/queryProcessSheet")
    @Operation(summary = "根据产品名称查询工艺单号")
    public BaseResponse<Map<String, Object>> queryProcessSheetByProductName(@RequestParam String productName) {
        Map<String, Object> result = processSheetService.queryProcessSheetByProductName(productName);
        return ResultUtils.success(result);
    }


    /**
     * 根据流程卡号查询批次信息（用于自动填充产品名称、数量、材料牌号）
     *
     * @param flowNumber 流程卡号
     * @return 批次信息，包含产品列表
     */
    @GetMapping("/query-by-flow-number")
    @Operation(summary = "根据流程卡号查询批次信息", description = "用于创建打印记录时自动填充产品信息")
    public BaseResponse<Map<String, Object>> queryByFlowNumber(@RequestParam String flowNumber) {
        return ResultUtils.success(printingRecordService.queryByFlowNumber(flowNumber));
    }
}